In [1]:
%load_ext autoreload
%autoreload 2
import os
import sys
import sklearn.metrics
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from collections import defaultdict
import vislab.datasets
import vislab.results
import vislab._results

In [2]:
label_df = vislab.datasets.flickr.get_df()
print vislab.datasets.flickr.underscored_style_names


['style_Detailed', 'style_Pastel', 'style_Melancholy', 'style_Noir', 'style_HDR', 'style_Vintage', 'style_Long_Exposure', 'style_Horror', 'style_Sunny', 'style_Bright', 'style_Hazy', 'style_Bokeh', 'style_Serene', 'style_Texture', 'style_Ethereal', 'style_Macro', 'style_Depth_of_Field', 'style_Geometric_Composition', 'style_Minimal', 'style_Romantic']

In [3]:
collection_name = 'flickr_mar23'
results_dirname = vislab.util.makedirs(vislab.config['paths']['shared_data'] + '/results')

In [4]:
c = vislab.util.get_mongodb_client()['predict'][collection_name]
# if c.find({'features': 'noise'}).count() > 0:
#     c.remove({'features': 'noise'})
pd.DataFrame([x for x in c.find()])


Out[4]:
_id data features num_test num_train num_val quadratic results_name score_test score_val task
0 532f9c839f00136077e981fc flickr_style_ALL [caffe_fc6] 16000 48000 16000 None data_flickr_style_ALL_features_['caffe_fc6']_n... 0.378437 0.405062 clf
1 532f9da69f00136077e98d09 flickr_style_ALL [caffe_fc7] 16000 48000 16000 None data_flickr_style_ALL_features_['caffe_fc7']_n... 0.375250 0.399545 clf
2 533004d49f00136077eaf1b1 flickr_style_ALL [mc_bit] 16000 48000 16000 None data_flickr_style_ALL_features_['mc_bit']_num_... 0.350235 0.361558 clf
3 53cdd6688bf4afb5a34ad5c7 flickr_style_ALL [fusion_flickr_mar23, pascal_mc_for_fusion_fli... 16000 48000 16000 None data_flickr_style_ALL_features_['fusion_flickr... 0.372812 0.393544 clf
4 53cdd9718bf4afb5a34ad5c8 flickr_style_ALL [fusion_flickr_mar23, pascal_mc_for_fusion_fli... 16000 48000 16000 fp data_flickr_style_ALL_features_['fusion_flickr... 0.373063 0.395062 clf
5 53ce07ba8bf4afb5a34ad5c9 flickr_style_ALL [fusion_flickr_mar23_all, pascal_mc_for_fusion... 16000 48000 16000 None data_flickr_style_ALL_features_['fusion_flickr... 0.394688 0.416098 clf
6 53ce09738bf4afb5a34ad5ca flickr_style_ALL [fusion_flickr_mar23_all, pascal_mc_for_fusion... 16000 48000 16000 fp data_flickr_style_ALL_features_['fusion_flickr... 0.391000 0.414994 clf

In [5]:
df, preds_panel = vislab._results.load_pred_results(
    collection_name, results_dirname,
    multiclass=True, force=False)

print preds_panel.shape
print preds_panel.items
print preds_panel.major_axis
print preds_panel.minor_axis

pred_prefix = 'pred'


Results in collection flickr_mar23: 7
(43, 80000, 5)
Index([u'importance', u'label', u'pred_style_Bokeh', u'pred_style_Bright', u'pred_style_Depth_of_Field', u'pred_style_Detailed', u'pred_style_Ethereal', u'pred_style_Geometric_Composition', u'pred_style_HDR', u'pred_style_Hazy', u'pred_style_Horror', u'pred_style_Long_Exposure', u'pred_style_Macro', u'pred_style_Melancholy', u'pred_style_Minimal', u'pred_style_Noir', u'pred_style_Pastel', u'pred_style_Romantic', u'pred_style_Serene', u'pred_style_Sunny', u'pred_style_Texture', u'pred_style_Vintage', u'split', u'style_Bokeh', u'style_Bright', u'style_Depth_of_Field', u'style_Detailed', u'style_Ethereal', u'style_Geometric_Composition', u'style_HDR', u'style_Hazy', u'style_Horror', u'style_Long_Exposure', u'style_Macro', u'style_Melancholy', u'style_Minimal', u'style_Noir', u'style_Pastel', u'style_Romantic', u'style_Serene', u'style_Sunny', u'style_Texture', u'style_Vintage'], dtype='object')
Index([u'10000032526', u'10000326754', u'10000830953', u'1000087620', u'10001451343', u'10001573306', u'10001822913', u'10002087334', u'10002847526', u'10003568386', u'10003574264', u'10004192933', u'10004268914', u'10004315046', u'10004454574', u'10004455234', u'10004457984', u'10004460194', u'10004467155', u'10004468814', u'10004482145', u'10004487845', u'10004490334', u'10004526193', u'10004624983', u'10004773083', u'10005049735', u'10005136676', u'10005137846', u'10005459576', u'10005593996', u'10006567464', u'10007199026', u'10007533796', u'10007546935', u'10007687716', u'10007818433', u'10008344915', u'10008362764', u'10008593574', u'10008818046', u'1000916033', u'10009232016', u'10009416433', u'10009428143', u'10009751665', u'10009898286', u'10010390723', u'10010846816', u'10011196343', u'10012568635', u'10013733776', u'10014324875', u'10014486544', u'10014552266', u'10014863874', u'10015131465', u'10015486443', u'10015616296', u'10015776294', u'10016478774', u'10017018623', u'10017165136', u'10017188074', u'10017414806', u'10017594706', u'10017606535', u'10017878724', u'10017915933', u'10018005846', u'10018214835', u'10018355815', u'10018525246', u'10018583583', u'10018718554', u'10018924934', u'10019028523', u'10019120933', u'10019125764', u'10019337243', u'10019463885', u'10020029975', u'10020032326', u'10020539106', u'10020679676', u'10020718036', u'10020756266', u'10020761354', u'10020771376', u'10020861183', u'10021049915', u'10021488366', u'10021507425', u'10021574406', u'10021601046', u'10021710306', u'10022271075', u'10022353754', u'10022411463', u'10022521056', ...], dtype='object')
Index([u'caffe_fc6 None vw', u'caffe_fc7 None vw', u'fusion_flickr_mar23,pascal_mc_for_fusion_flickr_mar23 fp vw', u'fusion_flickr_mar23_all,pascal_mc_for_fusion_flickr_mar23 fp vw', u'mc_bit None vw'], dtype='object')
/Users/sergeyk/anaconda/lib/python2.7/site-packages/pandas/util/decorators.py:53: FutureWarning: cols is deprecated, use subset instead
  warnings.warn(msg, FutureWarning)

One way to get binary predictions for Vintage: learn confidence threshold on val split


In [8]:
setting_name = 'caffe_fc6 None vw'
pred_df = preds_panel.minor_xs(setting_name)
gt_col = 'style_Vintage'

best_threshold, val_accs = vislab.results.learn_accuracy_threshold(
    pred_df[pred_df['split'] == 'val'], gt_col)

test_acc = vislab.results.pred_accuracy_at_threshold(
    pred_df[pred_df['split'] == 'test'], gt_col, best_threshold, verbose=True)
print("Accuracy on test, at threshold ({:.3f}) learned on val: {:.3f}".format(
    best_threshold, test_acc))


             precision    recall  f1-score   support

      False       0.77      0.72      0.75       800
       True       0.74      0.79      0.76       800

avg / total       0.76      0.76      0.75      1600

Accuracy on test, at threshold (-0.911) learned on val: 0.755

In [9]:
# Sanity check: are val preds lining up with test preds?
thresholds = np.logspace(-2, 0, 20) - 1
_, test_accs = vislab.results.learn_accuracy_threshold(
    pred_df[pred_df['split'] == 'test'], label_name, thresholds)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(thresholds, val_accs, 's-', label='val')
ax.plot(thresholds, test_accs, 'o--', label='test')
plt.legend()


Out[9]:
<matplotlib.legend.Legend at 0x12527dad0>

In [11]:
# Now find the accuracies for all labels and settings

cache_filename = '{}/{}_thresholds_and_accs.h5'.format(results_dirname, collection_name)
threshold_df, acc_df = vislab.results.learn_accuracy_thresholds_for_preds_panel(
    preds_panel, cache_filename)


....................
....................
....................
/Users/sergeyk/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py:1251: DeprecationWarning: createGroup() is pending deprecation, use create_group() instead. You may use the pt2to3 tool to update your source code.
  group = self._handle.createGroup(path, p)
/Users/sergeyk/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py:2464: DeprecationWarning: createArray() is pending deprecation, use create_array() instead. You may use the pt2to3 tool to update your source code.
  self._handle.createArray(self.group, key, value)
/Users/sergeyk/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py:2464: DeprecationWarning: createArray() is pending deprecation, use create_array() instead. You may use the pt2to3 tool to update your source code.
  self._handle.createArray(self.group, key, value)
/Users/sergeyk/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py:2464: DeprecationWarning: createArray() is pending deprecation, use create_array() instead. You may use the pt2to3 tool to update your source code.
  self._handle.createArray(self.group, key, value)
/Users/sergeyk/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py:2464: DeprecationWarning: createArray() is pending deprecation, use create_array() instead. You may use the pt2to3 tool to update your source code.
  self._handle.createArray(self.group, key, value)
/Users/sergeyk/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py:1251: DeprecationWarning: createGroup() is pending deprecation, use create_group() instead. You may use the pt2to3 tool to update your source code.
  group = self._handle.createGroup(path, p)
/Users/sergeyk/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py:2464: DeprecationWarning: createArray() is pending deprecation, use create_array() instead. You may use the pt2to3 tool to update your source code.
  self._handle.createArray(self.group, key, value)
/Users/sergeyk/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py:2464: DeprecationWarning: createArray() is pending deprecation, use create_array() instead. You may use the pt2to3 tool to update your source code.
  self._handle.createArray(self.group, key, value)
/Users/sergeyk/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py:2464: DeprecationWarning: createArray() is pending deprecation, use create_array() instead. You may use the pt2to3 tool to update your source code.
  self._handle.createArray(self.group, key, value)
/Users/sergeyk/anaconda/lib/python2.7/site-packages/pandas/io/pytables.py:2464: DeprecationWarning: createArray() is pending deprecation, use create_array() instead. You may use the pt2to3 tool to update your source code.
  self._handle.createArray(self.group, key, value)

In [19]:
acc_df.sort('caffe_fc6 None vw')


Out[19]:
caffe_fc6 None vw caffe_fc7 None vw mc_bit None vw
style_Romantic 0.680000 0.671875 0.667920
style_Depth_of_Field 0.685000 0.684375 0.699375
style_Serene 0.704375 0.703125 0.700000
style_Melancholy 0.708750 0.681875 0.682728
style_Texture 0.710000 0.699375 0.712406
style_Bright 0.733750 0.733750 0.686250
style_Pastel 0.745625 0.752500 0.707760
style_Detailed 0.752500 0.757500 0.756250
style_Vintage 0.755000 0.742500 0.719024
style_Geometric_Composition 0.777500 0.796875 0.772556
style_Bokeh 0.786875 0.792500 0.767544
style_Ethereal 0.806250 0.806875 0.768750
style_Hazy 0.807500 0.807500 0.819775
style_HDR 0.820000 0.815000 0.847309
style_Minimal 0.837500 0.870000 0.853750
style_Long_Exposure 0.841875 0.823750 0.819375
style_Horror 0.842500 0.821875 0.795000
style_Sunny 0.845625 0.830000 0.836875
style_Noir 0.852500 0.835625 0.844375
style_Macro 0.865625 0.881875 0.870301

20 rows × 3 columns


In [13]:
threshold_df


Out[13]:
caffe_fc6 None vw caffe_fc7 None vw mc_bit None vw
style_Bokeh -0.887116 -0.887116 -0.930481
style_Bright -0.887116 -0.911413 -0.930481
style_Depth_of_Field -0.911413 -0.930481 -0.887116
style_Detailed -0.887116 -0.887116 -0.887116
style_Ethereal -0.887116 -0.887116 -0.856155
style_Geometric_Composition -0.856155 -0.887116 -0.856155
style_HDR -0.887116 -0.887116 -0.856155
style_Hazy -0.816702 -0.887116 -0.856155
style_Horror -0.911413 -0.911413 -0.930481
style_Long_Exposure -0.856155 -0.887116 -0.911413
style_Macro -0.911413 -0.911413 -0.911413
style_Melancholy -0.887116 -0.911413 -0.887116
style_Minimal -0.887116 -0.887116 -0.945444
style_Noir -0.816702 -0.856155 -0.911413
style_Pastel -0.816702 -0.856155 -0.887116
style_Romantic -0.856155 -0.887116 -0.887116
style_Serene -0.856155 -0.856155 -0.856155
style_Sunny -0.887116 -0.856155 -0.887116
style_Texture -0.887116 -0.887116 -0.911413
style_Vintage -0.911413 -0.887116 -0.887116

20 rows × 3 columns

View AP and confusion matrix results


In [6]:
nice_feat_names = {
    # 'imagenet None vw': 'Imagenet',
    'caffe_fc6 None vw': 'Caffe FC6',
    'caffe_fc7 None vw': 'Caffe FC7',
    'mc_bit None vw': 'MC binary',
    'fusion_flickr_mar23_all,pascal_mc_for_fusion_flickr_mar23 fp vw': 'Late fusion x content',
    'random': 'Random'
}

mc_metrics = vislab.results.multiclass_metrics_feat_comparison(
    preds_panel, label_df, pred_prefix, features=preds_panel.minor_axis.tolist() + ['random'],
    balanced=True, with_plot=False, with_print=False, nice_feat_names=nice_feat_names)
ap_df = mc_metrics['ap_df'].copy()


Looks like the preds frame already has gt info.
Only taking 'test' split predictions.
********************caffe_fc6 None vw********************
Looks like the preds frame already has gt info.
Only taking 'test' split predictions.
********************caffe_fc7 None vw********************
Looks like the preds frame already has gt info.
Only taking 'test' split predictions.
********************fusion_flickr_mar23,pascal_mc_for_fusion_flickr_mar23 fp vw********************
Looks like the preds frame already has gt info.
Only taking 'test' split predictions.
********************fusion_flickr_mar23_all,pascal_mc_for_fusion_flickr_mar23 fp vw********************
Looks like the preds frame already has gt info.
Only taking 'test' split predictions.
********************mc_bit None vw********************
Looks like the preds frame already has gt info.
Only taking 'test' split predictions.
********************random********************
/Users/sergeyk/anaconda/lib/python2.7/site-packages/sklearn/metrics/metrics.py:1765: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.
  'precision', 'predicted', average, warn_for)

In [36]:
print mc_metrics['feat_metrics']['caffe_fc6 None vw'].keys()
mc_metrics['feat_metrics']['caffe_fc6 None vw']['binary_metrics_df']


['top_k_accuracies', 'conf_df_n', 'confusion_table_fig', 'results_df', 'binary_metrics_df', 'conf_df', 'accuracy']
Out[36]:
accuracy ap ap_sklearn auc mcc pr_fig results_df roc_fig
style_Bokeh 0.9491875 0.2534358 0.249593 0.8482118 0.1098038 None precision recall f1-score support ... None
style_Bright 0.9499375 0.2364833 0.2334051 0.8193616 0.1452619 None precision recall f1-score support ... None
style_Depth_of_Field 0.9499375 0.1521983 0.1485771 0.7343392 -0.001813747 None precision recall f1-score support ... None
style_Detailed 0.950875 0.2768284 0.2728357 0.8242256 0.1864095 None precision recall f1-score support ... None
style_Ethereal 0.952875 0.3925389 0.3911365 0.8827474 0.2738036 None precision recall f1-score support F... None
style_Geometric_Composition 0.9525 0.3546302 0.3491708 0.8606325 0.2613343 None precision recall f1-score support ... None
style_HDR 0.95325 0.4064259 0.402425 0.8906414 0.2740486 None precision recall f1-score support ... None
style_Hazy 0.9549375 0.4510523 0.4474077 0.8976615 0.3664856 None precision recall f1-score support ... None
style_Horror 0.95325 0.3962298 0.3941111 0.9022609 0.2562887 None precision recall f1-score support ... None
style_Long_Exposure 0.954625 0.4570856 0.451088 0.9014052 0.3115909 None precision recall f1-score support ... None
style_Macro 0.961 0.5820775 0.5771679 0.9448117 0.4963864 None precision recall f1-score support ... None
style_Melancholy 0.9500625 0.1469109 0.1449937 0.7687301 0.0344612 None precision recall f1-score support F... None
style_Minimal 0.954125 0.4435403 0.4398939 0.9147127 0.3447331 None precision recall f1-score support ... None
style_Noir 0.9554375 0.4810508 0.4741148 0.9337792 0.4049553 None precision recall f1-score support ... None
style_Pastel 0.9485625 0.244706 0.2419154 0.8234046 0.1144923 None precision recall f1-score support ... None
style_Romantic 0.949875 0.2043188 0.2006347 0.7523141 0.05625254 None precision recall f1-score support ... None
style_Serene 0.95175 0.2574657 0.2550262 0.7697465 0.1858527 None precision recall f1-score support ... None
style_Sunny 0.9553125 0.4806004 0.4761169 0.9155888 0.3937674 None precision recall f1-score support ... None
style_Texture 0.95 0.2270014 0.2218918 0.7800832 0.09804966 None precision recall f1-score support ... None
style_Vintage 0.950625 0.2725877 0.2699116 0.8369855 0.1376489 None precision recall f1-score support ... None
_mean 0.9524062 0.3358584 NaN 0.8500822 0.2224906 NaN NaN NaN

In [8]:
conf_df = mc_metrics['feat_metrics']['fusion_flickr_mar23_all,pascal_mc_for_fusion_flickr_mar23 fp vw']['conf_df'].astype(float)
conf_df.index = [x.replace('style_', '') for x in conf_df.index]
conf_df.columns = [x.replace('style_', '') for x in conf_df.columns]
fig = vislab.dataset_viz.plot_conditional_occurrence(conf_df, sort_by_prior=False, font_size=14)
fig.savefig('/Users/sergeyk/dropbox_work/aphrodite-writeup/figures/evaluation/flickr_conf.pdf', bbox_inches='tight')



In [43]:
ap_df = mc_metrics['ap_df']
column_order = ap_df.columns[(-ap_df.ix['_mean']).argsort().values]
ap_df.index = [x.replace('style_', '') for x in ap_df.index]
ap_df = ap_df.reindex_axis(column_order, axis=1)
ap_df.to_csv('/Users/sergeyk/dropbox_work/aphrodite-writeup/results/flickr_ap_df.csv')

fig = vislab.results_viz.plot_df_bar(ap_df, fontsize=14)
fig.savefig('/Users/sergeyk/dropbox_work/aphrodite-writeup/figures/flickr_ap_barplot.pdf', bbox_inches='tight')



In [44]:
ap_df


Out[44]:
Late fusion x content fusion_flickr_mar23,pascal_mc_for_fusion_flickr_mar23 fp vw Caffe FC6 Caffe FC7 MC binary Random
Bokeh 0.2853521 0.2740621 0.2534358 0.2620256 0.2483003 0.05175401
Bright 0.2419848 0.2279282 0.2364833 0.2296514 0.1831707 0.05143782
Depth_of_Field 0.1635604 0.159036 0.1521983 0.1513695 0.1471834 0.05437518
Detailed 0.332569 0.2866031 0.2768284 0.2767424 0.2790569 0.05337775
Ethereal 0.409966 0.3752282 0.3925389 0.3567508 0.3358961 0.05235483
Geometric_Composition 0.4013958 0.3866701 0.3546302 0.3882754 0.3579528 0.0524941
HDR 0.4887379 0.3855043 0.4064259 0.3703551 0.4749555 0.0513433
Hazy 0.4956275 0.4569149 0.4510523 0.4351383 0.4468359 0.05078288
Horror 0.4059032 0.3762911 0.3962298 0.3651908 0.2960974 0.05071572
Long_Exposure 0.5195801 0.444586 0.4570856 0.4265728 0.4622771 0.05320729
Macro 0.6164294 0.6006867 0.5820775 0.5933179 0.5297359 0.05115834
Melancholy 0.166 0.150515 0.1469109 0.1425287 0.1359279 0.0543633
Minimal 0.5096196 0.4702515 0.4435403 0.4751886 0.4808829 0.05178985
Noir 0.4846887 0.4626342 0.4810508 0.4512049 0.4090183 0.05427346
Pastel 0.2589971 0.2478822 0.244706 0.2363267 0.2109888 0.05259026
Romantic 0.2303622 0.210928 0.2043188 0.1967862 0.1849978 0.05371556
Serene 0.2803396 0.2611399 0.2574657 0.2545705 0.2381883 0.05189585
Sunny 0.5003281 0.4744235 0.4806004 0.4650306 0.4530627 0.05137262
Texture 0.2586567 0.2406193 0.2270014 0.2340977 0.229184 0.05419609
Vintage 0.2851511 0.265543 0.2725877 0.2487894 0.2216513 0.05113731
_mean 0.3667625 0.3378724 0.3358584 0.3279957 0.3162682 0.05241678

In [39]:
del ap_df['Random']
print ap_df.to_latex(float_format=lambda x: '%.3f'%x if not np.isnan(x) else '-')


\begin{tabular}{llllll}
\toprule
{} & Late fusion x content & fusion\_flickr\_mar23,pascal\_mc\_for\_fusion\_flickr\_mar23 fp vw & Caffe FC6 & Caffe FC7 & MC binary \\
\midrule
Bokeh                 &                 0.288 &                                              0.274 &     0.253 &     0.262 &     0.248 \\
Bright                &                 0.251 &                                              0.228 &     0.236 &     0.230 &     0.184 \\
Depth\_of\_Field        &                 0.169 &                                              0.159 &     0.152 &     0.151 &     0.148 \\
Detailed              &                 0.337 &                                              0.287 &     0.277 &     0.277 &     0.279 \\
Ethereal              &                 0.408 &                                              0.375 &     0.393 &     0.357 &     0.336 \\
Geometric\_Composition &                 0.411 &                                              0.387 &     0.355 &     0.388 &     0.358 \\
HDR                   &                 0.487 &                                              0.386 &     0.406 &     0.370 &     0.475 \\
Hazy                  &                 0.493 &                                              0.457 &     0.451 &     0.435 &     0.447 \\
Horror                &                 0.400 &                                              0.376 &     0.396 &     0.365 &     0.295 \\
Long\_Exposure         &                 0.515 &                                              0.445 &     0.457 &     0.427 &     0.463 \\
Macro                 &                 0.617 &                                              0.601 &     0.582 &     0.593 &     0.530 \\
Melancholy            &                 0.168 &                                              0.151 &     0.147 &     0.143 &     0.136 \\
Minimal               &                 0.512 &                                              0.470 &     0.444 &     0.475 &     0.481 \\
Noir                  &                 0.494 &                                              0.463 &     0.481 &     0.451 &     0.410 \\
Pastel                &                 0.258 &                                              0.248 &     0.245 &     0.236 &     0.211 \\
Romantic              &                 0.227 &                                              0.211 &     0.204 &     0.197 &     0.185 \\
Serene                &                 0.281 &                                              0.261 &     0.257 &     0.255 &     0.239 \\
Sunny                 &                 0.500 &                                              0.474 &     0.481 &     0.465 &     0.453 \\
Texture               &                 0.265 &                                              0.241 &     0.227 &     0.234 &     0.229 \\
Vintage               &                 0.282 &                                              0.266 &     0.273 &     0.249 &     0.222 \\
\_mean                 &                 0.368 &                                              0.338 &     0.336 &     0.328 &     0.316 \\
\bottomrule
\end{tabular}


In [31]:
from IPython.display import HTML
def top_k_images(df, k=10):
    return ' '.join(
        '<div style="display: inline-block;"><img src="{}" width="210px" /><br />{}</div>'.format(row['image_url'], row.name)
        for i, row in df[:k].iterrows())


import subprocess
import shlex
import re
def top_images_for_style(df, style, split=None):
    df_ = df.copy()
    if split is not None:
        df_ = df_[df_['split'] == split]
    title = '<h4>{}, results: {}</h4>'.format(
        style, df_.shape[0])
    df_ = df_.sort(style, ascending=False)
    
    # download and resize to folder
    d = os.path.expanduser('~/work/aphrodite-writeup/figures/flickr_on_flickr/')
    dirname = vislab.util.makedirs(d + '{}/'.format(style))
    w_dirname = vislab.util.makedirs(d + '{}/w/'.format(style))
    h_dirname = vislab.util.makedirs(d + '{}/h/'.format(style))
    counter = 0
    for i, row in df_[:5].iterrows():
        cmd = 'wget {} -O {}.jpg'.format(row['image_url'], counter)
        subprocess.call(shlex.split(cmd), cwd=dirname)
        
        cmd = 'find . -name "*.jpg" -depth 1 -exec convert {} -resize x310 -gravity Center -crop 192x310+0+0 -density 300 -units PixelsPerInch h/{} \;'
        subprocess.call(shlex.split(cmd), cwd=dirname)
        
        cmd = 'find . -name "*.jpg" -depth 1 -exec convert {} -resize 500 -gravity Center -crop 500x310+0+0 -density 300 -units PixelsPerInch w/{} \;'
        subprocess.call(shlex.split(cmd), cwd=dirname)
        counter += 1 
    
    return title + top_k_images(df_, k=5)

In [33]:
pred_df['image_url'] = label_df['image_url']
HTML('<h2>Flickr-learned style on all Pinterest data</h2>' + ' '.join(
    top_images_for_style(pred_df, 'pred_' + style, 'test')
    for style in vislab.datasets.flickr.underscored_style_names[3:]
))


Out[33]:

Flickr-learned style on all Pinterest data

pred_style_Noir, results: 16000


2490552447

12342857135

2070691205

2723952690

840272965

pred_style_HDR, results: 16000


12699525833

12946767463

12570883765

12495763844

12767460375

pred_style_Vintage, results: 16000


6942447188

5490181053

6348565207

4746918144

11518665565

pred_style_Long_Exposure, results: 16000


13237215684

13132947703

13219972013

13292845955

13187278403

pred_style_Horror, results: 16000


5134133727

4746487161

6813407669

3418855420

3713441887

pred_style_Sunny, results: 16000


11956641875

11725663524

12037953675

11854065203

11209911754

pred_style_Bright, results: 16000


12891312905

12745001984

13093585053

13095933265

9267891787

pred_style_Hazy, results: 16000


10313271063

13141228575

13163653005

13301692245

11882488835

pred_style_Bokeh, results: 16000


13058108564

13151098425

13276035754

13172347504

13153918983

pred_style_Serene, results: 16000


10877446493

10731311756

7913346608

9647545640

9495645307

pred_style_Texture, results: 16000


13309822394

12119048745

10613290465

10713993965

10808055815

pred_style_Ethereal, results: 16000


8507311626

9101186361

11218195066

8430110929

8727059410

pred_style_Macro, results: 16000


12615860105

13142203924

13161652183

13164949985

13154915685

pred_style_Depth_of_Field, results: 16000


12888341495

13290910995

12212504626

12306843663

10362809955

pred_style_Geometric_Composition, results: 16000


12413893764

11958515743

11904277194

11748040786

12116863724

pred_style_Minimal, results: 16000


9618243180

9324952359

12334549703

9587487841

12164529653

pred_style_Romantic, results: 16000


12888979223

9446092580

11709338875

9014955309

8330044860

Pinterest on Flickr


In [29]:
df, preds_panel = vislab._results.load_pred_results(
    'pinterest_80k_on_flickr_mar23', results_dirname,
    multiclass=True, force=True)

pred_prefix = 'pred'
print preds_panel.minor_axis


Results in collection pinterest_80k_on_flickr_mar23: 1
Index([u'caffe_fc6 False vw'], dtype='object')

In [31]:
# Get correct labels
dfs = []
for feat in preds_panel.minor_axis:
    df = preds_panel.minor_xs(feat)
    for col in label_df.columns:
        df[col] = label_df[col]
    dfs.append(df)
preds_panel = pd.Panel(dict(zip(preds_panel.minor_axis, dfs))).swapaxes('minor', 'items')

In [36]:
pinterest_on_flickr_mc_metrics = vislab.results.multiclass_metrics_feat_comparison(
    preds_panel, label_df, pred_prefix, features=preds_panel.minor_axis.tolist() + ['random'],
    balanced=True, with_plot=False, with_print=False, nice_feat_names=nice_feat_names)


Looks like the preds frame already has gt info.
Only taking 'test' split predictions.
********************caffe_fc6 False vw********************
Looks like the preds frame already has gt info.
Only taking 'test' split predictions.
********************random********************

In [37]:
pinterest_on_flickr_mc_metrics['ap_df']['caffe_fc6 False vw']


Out[37]:
style_Bokeh                     0.1649381
style_Bright                    0.1135498
style_Depth_of_Field            0.1235504
style_Detailed                  0.0687976
style_Ethereal                  0.2628365
style_Geometric_Composition     0.2405722
style_HDR                       0.3532396
style_Hazy                     0.08091952
style_Horror                    0.2672901
style_Long_Exposure             0.3084909
style_Macro                     0.4384734
style_Melancholy               0.09676803
style_Minimal                   0.2251937
style_Noir                      0.2983111
style_Pastel                    0.1928723
style_Romantic                  0.1370399
style_Serene                    0.1481406
style_Sunny                    0.05617466
style_Texture                   0.1754668
style_Vintage                   0.1348671
_mean                           0.1943746
Name: caffe_fc6 False vw, dtype: object

In [115]:
ap_df['Caffe FC6, Pinterest-trained'] = pinterest_on_flickr_mc_metrics['ap_df']['caffe_fc6 False vw']
ap_df['Difference'] = ap_df['Caffe FC6'] - ap_df['Caffe FC6, Pinterest-trained']
ap_df['Difference %'] = ap_df['Difference'] / ap_df['Caffe FC6']
ap_df['%'] = ap_df['Caffe FC6, Pinterest-trained'] / ap_df['Caffe FC6']
ap_df


Out[115]:
Caffe FC6 Caffe FC7 Random Caffe FC6, trained on Pinterest data Difference Caffe FC6, Pinterest-trained Difference % %
style_Depth_of_Field 0.1521983 0.1513695 0.05054119 0.1235504 0.02864796 0.1235504 0.1882278 0.8117722
style_Melancholy 0.1469109 0.1425287 0.05381399 0.09676803 0.05014285 0.09676803 0.3413148 0.6586852
style_Texture 0.2270014 0.2340977 0.05355861 0.1754668 0.05153463 0.1754668 0.2270234 0.7729766
style_Pastel 0.244706 0.2363267 0.05166539 0.1928723 0.05183372 0.1928723 0.2118204 0.7881796
style_HDR 0.4064259 0.3703551 0.05157514 0.3532396 0.05318636 0.3532396 0.1308636 0.8691364
style_Romantic 0.2043188 0.1967862 0.0520481 0.1370399 0.06727891 0.1370399 0.329284 0.670716
style_Bokeh 0.2534358 0.2620256 0.05278076 0.1649381 0.0884977 0.1649381 0.3491917 0.6508083
style_Serene 0.2574657 0.2545705 0.05061871 0.1481406 0.1093251 0.1481406 0.42462 0.57538
style_Geometric_Composition 0.3546302 0.3882754 0.05100994 0.2405722 0.114058 0.2405722 0.3216251 0.6783749
style_Bright 0.2364833 0.2296514 0.0526415 0.1135498 0.1229335 0.1135498 0.51984 0.48016
style_Horror 0.3962298 0.3651908 0.05496866 0.2672901 0.1289397 0.2672901 0.3254164 0.6745836
style_Ethereal 0.3925389 0.3567508 0.05398328 0.2628365 0.1297024 0.2628365 0.3304193 0.6695807
style_Vintage 0.2725877 0.2487894 0.05222592 0.1348671 0.1377206 0.1348671 0.5052342 0.4947658
_mean 0.3358584 0.3279957 0.05236048 0.1943746 0.1414838 0.1943746 0.4212602 0.5787398
style_Macro 0.5820775 0.5933179 0.05173692 0.4384734 0.1436042 0.4384734 0.2467097 0.7532903
style_Long_Exposure 0.4570856 0.4265728 0.05359206 0.3084909 0.1485947 0.3084909 0.3250916 0.6749084
style_Noir 0.4810508 0.4512049 0.0511736 0.2983111 0.1827396 0.2983111 0.379876 0.620124
style_Detailed 0.2768284 0.2767424 0.05251161 0.0687976 0.2080308 0.0687976 0.7514793 0.2485207
style_Minimal 0.4435403 0.4751886 0.05289927 0.2251937 0.2183466 0.2251937 0.4922813 0.5077187
style_Hazy 0.4510523 0.4351383 0.05310243 0.08091952 0.3701327 0.08091952 0.8205983 0.1794017
style_Sunny 0.4806004 0.4650306 0.05076258 0.05617466 0.4244257 0.05617466 0.8831157 0.1168843

21 rows × 8 columns


In [117]:
diff = ap_df.sort('%')['%']
diff_top = diff[diff >= np.percentile(diff, 75)]
print('Styles that are most transferrable from Pinterest to Flickr (mean Pinterest-trained AP: {:.1f}% of Flickr-trained AP): {}.'.format(
    diff_top.values.mean() * 100,
    ', '.join([_[6:] for _ in sorted(diff_top.index.tolist())]),
))
diff_bottom = diff[diff <= np.percentile(diff, 25)]
print('Styles that are least transferrable from Pinterest to Flickr (mean {:.1f}% of Flickr-trained AP): {}.'.format(
    diff_bottom.values.mean() * 100,
    ', '.join([_[6:] for _ in sorted(diff_bottom.index.tolist())]),
))


Styles that are most transferrable from Pinterest to Flickr (mean Pinterest-trained AP: 77.9% of Flickr-trained AP): Depth_of_Field, Geometric_Composition, HDR, Macro, Pastel, Texture.
Styles that are least transferrable from Pinterest to Flickr (mean 33.8% of Flickr-trained AP): Bright, Detailed, Hazy, Minimal, Sunny, Vintage.